# -*- coding: utf-8 -*-
from git import Repo
import sys
import platform
import time
import socket
import getpass
import os

version_info_path = 'src/include/'
version_info_file = 'version_info.h'

info_str = '#ifndef _VERSION_INFO_H\n#define _VERSION_INFO_H\n'
repo = Repo('.')

info_str += '\n\n//////////////////////////////////////////////////////////git base info//////////////////////////////////////////////////////////\n'
git_base = str(repo.active_branch)
info_str += '#define GIT_BASE_INFO_ACTIVE_BRANCH "' + git_base + '" /* >>>active_branch */\n'

#https://git-scm.com/docs/pretty-formats
#%H  abbreviated commit hash    
#%h  commit hash    
#%T  tree hash    
#%t  abbreviated tree hash    
#%P  parent hashes    
#%p  abbreviated parent hashes    
#%an  author name    
#%aN  author name (respecting .mailmap, see git-shortlog[1] or git-blame[1])    
#%ae  author email    
#%aE  author email (respecting .mailmap, see git-shortlog[1] or git-blame[1])    
#%al  author email local-part (the part before the @ sign)    
#%aL  author local-part (see 
#%al) respecting .mailmap, see git-shortlog[1] or git-blame[1])    
#%ad  author date (format respects --date= option)    
#%aD  author date, RFC2822 style    
#%ar  author date, relative    
#%at  author date, UNIX timestamp    
#%ai  author date, ISO 8601-like format    
#%aI  author date, strict ISO 8601 format    
#%as  author date, short format (YYYY-MM-DD)    
#%ah  author date, human style (like the --date=human option of git-rev-list[1])    
#%cn  committer name    
#%cN  committer name (respecting .mailmap, see git-shortlog[1] or git-blame[1])    
#%ce  committer email    
#%cE  committer email (respecting .mailmap, see git-shortlog[1] or git-blame[1])    
#%cl  committer email local-part (the part before the @ sign)    
#%cL  committer local-part (see %cl) respecting .mailmap, see git-shortlog[1] or git-blame[1])    
#%cd  committer date (format respects --date= option)    
#%cD  committer date, RFC2822 style    
#%cr  committer date, relative    
#%ct  committer date, UNIX timestamp    
#%ci  committer date, ISO 8601-like format    
#%cI  committer date, strict ISO 8601 format    
#%cs  committer date, short format (YYYY-MM-DD)    
#%ch  committer date, human style (like the --date=human option of git-rev-list[1])    
#%d  ref names, like the --decorate option of git-log[1]    
#%D  ref names without the " (", ")" wrapping.    
#%(describe[:options])  human-readable name, like git-describe[1]; empty string for undescribable commits. The describe string may be followed by a colon and zero or more comma-separated options. Descriptions can be inconsistent when tags are added or removed at the same time.    match=<pattern>: Only consider tags matching the given glob(7) pattern, excluding the "refs/tags/" prefix.    exclude=<pattern>: Do not consider tags matching the given glob(7) pattern, excluding the "refs/tags/" prefix.    
#%S  ref name given on the command line by which the commit was reached (like git log --source), only works with git log    
#%e  encoding    
#%s  subject    
#%f  sanitized subject line, suitable for a filename    
#%b  body    
#%B  raw body (unwrapped subject and body)    
#%N  commit notes    
#%GG  raw verification message from GPG for a signed commit    
#%G?  show "G" for a good (valid) signature, "B" for a bad signature, "U" for a good signature with unknown validity, "X" for a good signature that has expired, "Y" for a good signature made by an expired key, "R" for a good signature made by a revoked key, "E" if the signature cannot be checked (e.g. missing key) and "N" for no signature    
#%GS  show the name of the signer for a signed commit    
#%GK  show the key used to sign a signed commit    
#%GF  show the fingerprint of the key used to sign a signed commit    
#%GP  show the fingerprint of the primary key whose subkey was used to sign a signed commit    
#%GT  show the trust level for the key used to sign a signed commit    
#%gD  reflog selector, e.g., refs/stash@{1} or refs/stash@{2 minutes ago}; the format follows the rules described for the -g option. The portion before the @ is the refname as given on the command line (so git log -g refs/heads/master would yield refs/heads/master@{0}).    
#%gd  shortened reflog selector; same as 
#%gD, but the refname portion is shortened for human readability (so refs/heads/master becomes just master).    
#%gn  reflog identity name    
#%gN  reflog identity name (respecting .mailmap, see git-shortlog[1] or git-blame[1])    
#%ge  reflog identity email    
#%gE  reflog identity email (respecting .mailmap, see git-shortlog[1] or git-blame[1])    
#%gs  reflog subject
info_str += '\n\n//////////////////////////////////////////////////////////git log info//////////////////////////////////////////////////////////\n'
log = repo.git.log('-1','--pretty=format:%H')
info_str += '#define GIT_LOG_INFO_COMMIT_HASH "' + log + '" /* >>>%H commit hash */\n'
log = repo.git.log('-1','--pretty=format:%h')
info_str += '#define GIT_LOG_INFO_ABBREVIATED_COMMIT_HASH "' + log + '" /* >>>%h abbreviated commit hash */\n'
log = repo.git.log('-1','--pretty=format:%T')
info_str += '#define GIT_LOG_INFO_TREE_HASH "' + log + '" /* >>>%T tree hash */\n'
log = repo.git.log('-1','--pretty=format:%t')
info_str += '#define GIT_LOG_INFO_ABBREVIATED_TREE_HASH "' + log + '" /* >>>%t abbreviated tree hash */\n'
log = repo.git.log('-1','--pretty=format:%P')
info_str += '#define GIT_LOG_INFO_PARENT_HASH "' + log + '" /* >>>%P parent hashs */\n'
log = repo.git.log('-1','--pretty=format:%p')
info_str += '#define GIT_LOG_INFO_ABBREVIATED_PARENT_HASHS "' + log + '" /* >>>%p abbreviated parent hashs */\n'
log = repo.git.log('-1','--pretty=format:%an')
info_str += '#define GIT_LOG_INFO_AUTHOR_NAME "' + log + '" /* >>>%an author name */\n'
log = repo.git.log('-1','--pretty=format:%aN')
info_str += '#define GIT_LOG_INFO_AUTHOR_NAME_MAILMAP "' + log + '" /* >>>%aN author name (respecting .mailmap, see git-shortlog[1] or git-blame[1]) */\n'
log = repo.git.log('-1','--pretty=format:%ae')
info_str += '#define GIT_LOG_INFO_AUTHOR_EMAIL "' + log + '" /* >>>%ae author email */\n'
log = repo.git.log('-1','--pretty=format:%aE')
info_str += '#define GIT_LOG_INFO_AUTHOR_EMAIL_MAILMAP "' + log + '" /* >>>%aE author email (respecting .mailmap, see git-shortlog[1] or git-blame[1]) */\n'
log = repo.git.log('-1','--pretty=format:%al')
info_str += '#define GIT_LOG_INFO_AUTHOR_EMAIL_LOCAL_PART "' + log + '" /* >>>%al author email local-part (the part before the @ sign) */\n'
log = repo.git.log('-1','--pretty=format:%aL')
info_str += '#define GIT_LOG_INFO_AUTHOR_EMAIL_LOCAL_PART_MAILMAP "' + log + '" /* >>>%aL author local-part (see %al) respecting .mailmap, see git-shortlog[1] or git-blame[1]) */\n'
log = repo.git.log('-1','--pretty=format:%ad')
info_str += '#define GIT_LOG_INFO_AUTHOR_DATA "' + log + '" /* >>>%ad author date (format respects --date= option) */\n'
log = repo.git.log('-1','--pretty=format:%aD')
info_str += '#define GIT_LOG_INFO_AUTHOR_DATA_RFC2822 "' + log + '" /* >>>%aD author date, RFC2822 style */\n'
log = repo.git.log('-1','--pretty=format:%ar')
info_str += '#define GIT_LOG_INFO_AUTHOR_DATA_RELATIVE "' + log + '" /* >>>%ar author date, relative */\n'
log = repo.git.log('-1','--pretty=format:%at')
info_str += '#define GIT_LOG_INFO_AUTHOR_DATA_UNIX_TIMESTAMP "' + log + '" /* >>>%at author date, UNIX timestamp */\n'
log = repo.git.log('-1','--pretty=format:%ai')
info_str += '#define GIT_LOG_INFO_AUTHOR_DATA_ISO_8601 "' + log + '" /* >>>%ai author date, ISO 8601-like format */\n'
log = repo.git.log('-1','--pretty=format:%aI')
info_str += '#define GIT_LOG_INFO_AUTHOR_DATA_SHORT_FORMAT "' + log + '" /* >>>%aI author date, short format (YYYY-MM-DD) */\n'
log = repo.git.log('-1','--pretty=format:%as')
info_str += '#define GIT_LOG_INFO_AUTHOR_DATA_HUMAN "' + log + '" /* >>>%as author date, human style (like the --date=human option of git-rev-list[1]) */\n'
log = repo.git.log('-1','--pretty=format:%cn')
info_str += '#define GIT_LOG_INFO_COMMITTER_NAME "' + log + '" /* >>>%cn committer name */\n'
log = repo.git.log('-1','--pretty=format:%cN')
info_str += '#define GIT_LOG_INFO_COMMITTER_NAME_MAILMAP "' + log + '" /* >>>%cn committer name (respecting .mailmap, see git-shortlog[1] or git-blame[1]) */\n'
log = repo.git.log('-1','--pretty=format:%ce')
info_str += '#define GIT_LOG_INFO_COMMITTER_EMAIL "' + log + '" /* >>>%ce committer email */\n'
log = repo.git.log('-1','--pretty=format:%cE')
info_str += '#define GIT_LOG_INFO_COMMITTER_EMAIL_MAILMAP "' + log + '" /* >>>%cE committer email (respecting .mailmap, see git-shortlog[1] or git-blame[1]) */\n'
log = repo.git.log('-1','--pretty=format:%cl')
info_str += '#define GIT_LOG_INFO_COMMITTER_EMAIL_LOCAL_PART "' + log + '" /* >>>%cl committer email local-part (the part before the @ sign) */\n'
log = repo.git.log('-1','--pretty=format:%cL')
info_str += '#define GIT_LOG_INFO_COMMITTER_EMAIL_LOCAL_PART_MAILMAP "' + log + '" /* >>>%cL committer email local-part (respecting .mailmap, see git-shortlog[1] or git-blame[1]) */\n'
log = repo.git.log('-1','--pretty=format:%cd')
info_str += '#define GIT_LOG_INFO_COMMITTER_DATA "' + log + '" /* >>>%ad committer date (format respects --date= option) */\n'
log = repo.git.log('-1','--pretty=format:%cD')
info_str += '#define GIT_LOG_INFO_COMMITTER_DATA_RFC2822 "' + log + '" /* >>>%aD committer date, RFC2822 style */\n'
log = repo.git.log('-1','--pretty=format:%cr')
info_str += '#define GIT_LOG_INFO_COMMITTER_DATA_RELATIVE "' + log + '" /* >>>%ar committer date, relative */\n'
log = repo.git.log('-1','--pretty=format:%ct')
info_str += '#define GIT_LOG_INFO_COMMITTER_DATA_UNIX_TIMESTAMP "' + log + '" /* >>>%at committer date, UNIX timestamp */\n'
log = repo.git.log('-1','--pretty=format:%ci')
info_str += '#define GIT_LOG_INFO_COMMITTER_DATA_ISO_8601 "' + log + '" /* >>>%ai committer date, ISO 8601-like format */\n'
log = repo.git.log('-1','--pretty=format:%cI')
info_str += '#define GIT_LOG_INFO_COMMITTER_DATA_SHORT_FORMAT "' + log + '" /* >>>%aI committer date, short format (YYYY-MM-DD) */\n'
log = repo.git.log('-1','--pretty=format:%cs')
info_str += '#define GIT_LOG_INFO_COMMITTER_DATA_HUMAN "' + log + '" /* >>>%as committer date, human style (like the --date=human option of git-rev-list[1]) */\n'

info_str += '\n\n//////////////////////////////////////////////////////////build info//////////////////////////////////////////////////////////\n'
git_base = str(repo.active_branch)
info_str += '#define BUILD_INFO_BUILD_TIME "' + time.asctime(time.localtime()) + '" /* >>>build time */\n'
info_str += '#define BUILD_INFO_SYS_PLATFORM "' + sys.platform + '" /* >>>sys platform */\n'
#info_str += '#define BUILD_INFO_PLATFORM_ARCHITECTURE "' + platform.architecture() + '" /* >>>platform architecture */\n'
info_str += '#define BUILD_INFO_PLATFORM_PLATFORM "' + platform.platform() + '" /* >>>platform platform */\n'
info_str += '#define BUILD_INFO_PLATFORM_SYSTEM "' + platform.system() + '" /* >>>platform system */\n'
info_str += '#define BUILD_INFO_HOST_NAME "' + socket.gethostname() + '" /* >>>host name */\n'
info_str += '#define BUILD_INFO_USER_NAME "' + getpass.getuser() + '" /* >>>user name */\n'


info_str += '\n\n#endif	/* >>_VERSION_INFO_H<< */\n /* EOF */\n'
path_exists = os.path.exists(version_info_path)
print(path_exists)
if not path_exists:
	os.makedirs(version_info_path) 
#fi = open(version_info_path + version_info_file, 'w')
#fi.write(info_str)
#fi.close()

print('update version info success!')